﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;

namespace DatabaseInterface
{
    public class GeneralInfo
    {
        SqlConnection cnx;
        public GeneralInfo(Connection cnx)
        {
            this.cnx = cnx.getConnection();
        }
        public DateTime GetDateFromHour(int year, int hour)
        {
            DateTime date = new DateTime(year, 1, 1);
            date.AddHours(hour);
            return date;
        }
        public double GetTotalDispatch(DateTime date)
        {
            try
            {
                String cmdText = "SELECT SUM(Outgoing) FROM PowerUnitProduction WHERE Date = @Date AND Hour = @Hour";
                SqlCommand cmd = new SqlCommand(cmdText, this.cnx);

                cmd.Parameters.Add("@Date", System.Data.SqlDbType.Date);
                cmd.Parameters["@Date"].Value = date.Date;

                cmd.Parameters.Add("@Hour", System.Data.SqlDbType.Int);
                cmd.Parameters["@Hour"].Value = date.Hour + 1;

                double totalDispatch = 0;
                cnx.Open();
                object result = cmd.ExecuteScalar();
                cnx.Close();

                if (result != DBNull.Value)
                {
                    totalDispatch = Convert.ToDouble(result);
                }
                return totalDispatch;
                
            }
            catch (Exception ex)
            {
                if (cnx.State != System.Data.ConnectionState.Closed)
                {
                    cnx.Close();
                }
                throw ex;
                
            }

        }
    }
}
